import java.util.Arrays;

public class MyStack {
    //int类型的数组
    private int[] elem;
    //有效数组的长度
    private int useSized;
    //默认长度为10
    private static final int DEFAULT_CAPACITY = 10;
    //创建一个默认长度的数组
    public MyStack(){
        this.elem = new int[DEFAULT_CAPACITY];
    }
    //push()方法
    public void push(int val){
        if(is_Full()){
            //栈满进行扩容
            Arrays.copyOf(elem,2*elem.length);
        }
        //在useSized处放入元素
        elem[useSized] = val;
        useSized++;
    }

    private boolean is_Full() {
    if(useSized >= elem.length){
        return true;
        }
    return false;
    }
    //isEmpty()
    public  boolean isEmpty(){
        return useSized == 0;
    }
    public int pop(){
        //判断栈是否为空
        if(isEmpty()){
            throw new RuntimeException("栈中没有元素");
        }
        //记录下原来的最后一个元素的下标
        int beforesize = useSized - 1;
        useSized--;
        return elem[beforesize];
    }
    //peek()方法
    public int peek(){
        if(isEmpty()){
            throw new RuntimeException("栈中没有元素");
        }
        return elem[useSized-1];
    }
}
